﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.AddIn.Pipeline;
using RootfusHostView;
using RootfusAddinContracts;

namespace RootfusAddinHostAdapters
{
    /// <summary>
    /// Maps the contract over to our view. Ick!
    /// </summary>
    [HostAdapter]
    class RootfusSurfaceItemControlToHostViewAdaptor : RootfusHostView.RootfusSurfaceItem
    {
        private RootfusAddinContracts.IRootfusSurfaceItemContract _contract;
        private ContractHandle _handle;

        public RootfusSurfaceItemControlToHostViewAdaptor(RootfusAddinContracts.IRootfusSurfaceItemContract contract)
        {
            _contract = contract;
            _handle = new ContractHandle(_contract);
        }

        /// <summary>
        /// Returns the UI element that is the default display canvas.
        /// </summary>
        /// <returns></returns>
        public override System.Windows.FrameworkElement SurfaceUI
        {
            get { return FrameworkElementAdapters.ContractToViewAdapter(_contract.SurfaceUI); }
        }

        /// <summary>
        /// Return the configuration UI
        /// </summary>
        public override System.Windows.FrameworkElement ConfigUI
        {
            get { return FrameworkElementAdapters.ContractToViewAdapter(_contract.ConfigUI); }
        }

        /// <summary>
        /// Is there even a settings UI?
        /// </summary>
        public override bool HasSettingsUI
        {
            get { return _contract.HasSettingsUI; }
        }

        /// <summary>
        /// The list of input pins.
        /// </summary>
        public override IList<PinDefinition> InputPins
        {
            get
            {
                return CollectionAdapters.ToIList<IPinDefinitionContract,PinDefinition>(_contract.InputPins,
                    PinDefinitionAddInAdapter.ContractToViewAdapter,
                    PinDefinitionAddInAdapter.ViewToContractAdapter);
            }
        }

        /// <summary>
        /// The list of output pins.
        /// </summary>
        public override IList<string> OutputPinNames
        {
            get
            {
                return CollectionAdapters.ToIList<string>(_contract.OutputPinNames);
            }
        }

        /// <summary>
        /// Call to cause add-in to update itself.
        /// </summary>
        public override void Update()
        {
            _contract.Update();
        }

        /// <summary>
        /// Stash, locally, the callback.
        /// </summary>
        /// <param name="callback"></param>
        public override void SetCallback(SICallback callback)
        {
            _contract.SetCallback(new SICallbackViewToContract(callback));
        }

        /// <summary>
        /// Get the pin data from our home object!
        /// </summary>
        /// <param name="pin"></param>
        /// <returns></returns>
        public override RootfusHostView.ROOTObjectSpec GetPinDataAsROOTObject(string pinname)
        {
            RootfusAddinContracts.ROOTObjectSpec rtn = _contract.GetPinDataAsROOTObject(pinname);
            return UtilityConverters.ConvertROOTObjectSpec(rtn);
        }

        /// <summary>
        /// Configuration is done! Let them know!
        /// </summary>
        public override void ConfigDone()
        {
            _contract.ConfigDone();
        }

        public override void SetConfigXML(string xml_config)
        {
            _contract.SetConfigXML(xml_config);
        }

        public override bool SurfaceUIResizeAllowed
        {
            get { return _contract.SurfaceUIResizeAllowed; }
        }

        public override void RequestResize(double width, double height)
        {
            _contract.RequestResize(width, height);
        }
    }
}
